﻿using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class JavaScriptGenerator
    {
        public static void Generate(DatabaseTableSchema form)
        {
            var sb = new StringBuilder();
            DefaultHeader(form, sb);
            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("\\Public\\api\\" + form.TableName + "\\") + "List_Generated.js", sb.ToString());
        }

        private static void DefaultHeader(DatabaseTableSchema form, StringBuilder sb)
        {
            sb.AppendLine("define(['util', 'ajax'], function (util, ajax) {");
            sb.AppendLine("\t\"use strict\";");
            sb.AppendLine("");
            sb.AppendLine("\tvar exports = {},");
            sb.AppendLine("\t\tViewModel;");
            sb.AppendLine("");
            sb.AppendLine("\tViewModel = function (data) {");
            sb.AppendLine("");
            sb.AppendLine("\t};");
            sb.AppendLine("");
            sb.AppendLine("\texports.exec = function (model) {");
            sb.AppendLine("\t\tvar viewModel = new ViewModel(model);");
            sb.AppendLine("\t\tko.applyBindings(viewModel);");
            sb.AppendLine("\t};");
            sb.AppendLine("");
            sb.AppendLine("\treturn exports;");
            sb.AppendLine("");
            sb.AppendLine("});");
        }
    }
}
